function err_exit {
    echo "----Install Error: $1 -----------"
    exit 1
}

function php_beast_ins {

    # 源码包路径
    srcPath=$1
    if [ ! -f "$srcPath" ]; then
        err_exit "not found source package."
    fi

    # 检测是否.tar.gz后缀
    srcName=${srcPath##*/}
    packageName=${srcName/%.tar.gz/}
    packageName=${packageName/%.tgz/}
    if [ "$srcName"x = "$packageName"x ];then
        err_exit "not found .tar.gz file."
    fi

    # 源码包路径
    srcDir=${srcPath%/*}

    # 程序名(没版本号)
    pkgName=${packageName/%-*}

    #todo 获取安装目录
    installPath=${2:-"/usr/local/${packageName}"}

    #todo php安装路径
    if [ ! -d $PHP_INSTALL_PATH ]; then
        err_exit "not found PHP_INSTALL_PATH."
    fi

    # 日志路径
    IN_LOG=/tmp/${pkgName}_install.log
    echo "">$IN_LOG
    if [ "$?" != "0" ]; then
        err_exit "create log file."
    fi

    echo
    echo "$packageName installing..."

    # 进入源码包目录
    cd $srcDir

    # 清除文件
    rm -Rf $packageName

    # 解压源码包
    tar -zxvf $srcPath > $IN_LOG 2>&1
    cd $packageName

    #todo 初始化configure文件
    echo "$packageName phpize..."
    ${PHP_INSTALL_PATH}/bin/phpize
    if [ "$?" != "0" ]; then
        err_exit "$packageName phpize err"
    fi

    #todo 编译配置
    echo "$packageName configure..."
    ./configure --with-php-config=$PHP_INSTALL_PATH/bin/php-config >>$IN_LOG 2>&1
    if [ "$?" != "0" ]; then
        err_exit "$packageName configure err"
    fi

    # 编译开始
    echo "$packageName make..."
    make >> $IN_LOG 2>&1
    if [ "$?" != "0" ]; then
       err_exit "$packageName make err"
    fi

    # 安装开始
    echo "$packageName make install..."
    make install >> $IN_LOG 2>&1
    if [ "$?" != "0" ]; then
        err_exit "$packageName make install err"
    fi

    # 清除源码包目录
    cd $srcDir
    rm -Rf $packageName

    echo "$packageName complete."
}

php_beast_ins $*
